package com.example.lx6_8

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.aa.mvvm.BaseActivity
import com.example.lx6_8.adapter.GoodsAdapter
import com.example.lx6_8.adapter.OneAdapter
import com.example.lx6_8.adapter.TwoAdapter
import com.example.lx6_8.cate.CateViewModel
import com.example.lx6_8.databinding.ActivityMain2Binding
import com.example.lx6_8.databinding.ActivityMain3Binding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "/app/MainActivity3")
class MainActivity3 : BaseActivity<ActivityMain3Binding,CateViewModel>() {

    val oneAdapter by lazy { OneAdapter() }
    val twoAdapter by lazy { TwoAdapter() }
    val goodsAdapter by lazy { GoodsAdapter() }
    override fun initView() {
        binding.rv1.apply {
            adapter = oneAdapter
            layoutManager = LinearLayoutManager(this@MainActivity3)
        }
        binding.rv2.apply {
            adapter = twoAdapter
            layoutManager = GridLayoutManager(this@MainActivity3,4)
        }
        binding.rvGoods.apply {
            adapter = goodsAdapter
            layoutManager = GridLayoutManager(this@MainActivity3,2)
        }
        vm.getCate()

        oneAdapter.setOnItemClickListener { adapter, view, position ->
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()
        }

        vm.getGoods(534,10,1)
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            vm.getGoods(twoAdapter.data[position].id,10,1)
        }

    }

    override fun initData() {
        vm.cateData.observe(this){
            oneAdapter.data.clear()
            oneAdapter.data.addAll(it)
            oneAdapter.notifyDataSetChanged()

            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[0].child)
            twoAdapter.notifyDataSetChanged()
        }

        vm.goodsData.observe(this){
            goodsAdapter.data.clear()
            goodsAdapter.data.addAll(it.list)
            goodsAdapter.notifyDataSetChanged()
        }
    }
}